< previous page page_409 next page >

Page 409
top. The program can find hexagons near the bottom almost instantly, but it takes more than a second to find those at the top.
Quadtrees
You can make both the display and location of objects faster using a quadtree. A quadtree is a tree that recursively divides parts of world coordinate space into four segments. The four segments represent the upper-left, upper-right, lower-left, and lower-right quadrants of the drawing area. The four segments are also referred to as the northwest, northeast, southwest, and southeast quadrants.
Each node in a quadtree contains a list of the objects that are within its part of the display area. If a node contains too many objects, it is further divided into four subnodes, and the items are assigned to the proper quadrants. This process is repeated until each node contains at most some predetermined maximum number of objects.
Figure 12.23 shows how a small quadtree represents a picture containing six objects. Nodes in this quadtree have been divided until no node contains more than two items.
Once you have created a quadtree, you can use it to help you decide which objects to draw. Figure 12.24 shows a world window drawn on top of the quadtree from Figure 12.23. The first quadtree node contains four subnodes. Because the world window overlaps only the northwest subnode, you need to examine only that subnode when you are drawing the objects in the world window. By not considering the other three nodes in the quadtree, you remove three of the picture's objects (D, E, and F) from consideration. You do not need to draw these objects or examine them further.
The northwest subnode contains four subnodes of its own. The world window intersects all four of these nodes, so you must draw all of the objects within them. Those nodes contain the picture objects A, B, and C, so you would draw those three objects. Only two, A and B, actually lie within the world window, so Visual Basic must clip object C out of the picture.
Locating Objects
Quadtrees are also helpful in locating objects. Suppose a user clicks on object C in Figure 12.24. When you examine the first quadtree node, you find that the point the user
z0429-01.gif
Figure 12.23
Dividing a display area with a quadtree.

 
< previous page page_409 next page >